package com.etang.a2020c1010.fragment;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.etang.a2020c1010.R;

import java.util.ArrayList;
import java.util.List;

public class BarActivity extends FragmentActivity {

    ViewPager pager;
    ListCheck listCheck;
    List<Fragment> is_fragment = new ArrayList<Fragment>();
    RadioButton ra_base, ra_link;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar);
        is_fragment.add(new BaseFragment());
        is_fragment.add(new LinkFragment());
        initView();
        ra_link.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(1);
            }
        });
        ra_base.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(0);
            }
        });
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position) {
                    case 0:
                        ra_base.setChecked(true);
                        break;
                    case 1:
                        ra_link.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        pager = (ViewPager) findViewById(R.id.pager);
        listCheck = new ListCheck(getSupportFragmentManager());
        pager.setAdapter(listCheck);
        pager.setOffscreenPageLimit(is_fragment.size());
        ra_base = (RadioButton) findViewById(R.id.ra_base);
        ra_link = (RadioButton) findViewById(R.id.ra_link);
    }

    public class ListCheck extends FragmentPagerAdapter {

        public ListCheck(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return is_fragment.get(position);
        }

        @Override
        public int getCount() {
            return is_fragment.size();
        }
    }

}